﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Utils;


namespace Vote.Filters
{
    public class WeixinFilter : ActionFilterAttribute
    {
        /// <summary>
        /// 动作执行之前先获取用户的微信信息
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (HttpContext.Current.Session["OpenId"] == null)
            {
                #region 加负载均衡要去掉端口
                string returnUrl = filterContext.RequestContext.HttpContext.Request.Url.ToString();
                int port=filterContext.RequestContext.HttpContext.Request.Url.Port;
                string portStr = ":" + port;
                returnUrl = returnUrl.Replace(portStr,"");
                #endregion

                string weixinDomain = ConfigurationManager.AppSettings["WeixinDomain"];

                HttpContext.Current.Response.Redirect(weixinDomain + "/weixin/GetWeixinUserInfo?returnUrl=" + returnUrl);
            }

            base.OnActionExecuting(filterContext);
        }
    }
}